from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from PIL import Image
import urllib.request
from Selenium import chaojiying

# def get_track(distance):
# 	track = []
# 	current = 0
# 	mid = distance * 3 / 4
# 	t = 0.2
# 	v = 0
# 	while current < distance:
# 		if current < mid:
# 			a = 2
# 		else:
# 			a = -3
# 		v0 = v
# 		v = v0 + a * t
# 		move = v0 * t + 1 / 2 * a * t * t
# 		current += move
# 		track.append(round(move))
# 	return track

if __name__ == '__main__':
    path = "chromedriver.exe"
    liu = webdriver.Chrome(path)
    url = "https://www.jd.com/"
    liu.get(url)
    time.sleep(5)
    btdl = liu.find_element_by_xpath("//div[@class='login-form']/div[3]/a")
    btdl.click()
    time.sleep(2)
    etname = liu.find_element_by_xpath("//form[@id='formlogin']//input[@id='loginname']")
    etname.send_keys("17866947052")
    time.sleep(2)
    etpwd = liu.find_element_by_xpath("//form[@id='formlogin']//input[@id='nloginpwd']")
    etpwd.send_keys("zyj141247830")
    time.sleep(2)
    btsum = liu.find_element_by_xpath("//form[@id='formlogin']//a[@id='loginsubmit']")
    btsum.click()
    time.sleep(2)
    img_src = liu.find_element_by_xpath("//div[@class='JDJRV-bigimg']/img")
    urllib.request.urlretrieve(url=img_src.get_attribute("src"), filename="jdyzm.png")
    time.sleep(2)
    cjy = chaojiying.Chaojiying_Client('action', 'action', '96001')
    cjy_img = open('jdyzm.png', 'rb').read()
    cjy_xy = cjy.PostPic(cjy_img, 9101)
    cjy_z = cjy_xy.get("pic_str")
    list_xy = cjy_z.split(",")
    time.sleep(2)
    # action = ActionChains(liu)
    # action.move_to_element_with_offset(img_src,5,int(list_xy[1])-10).click_and_hold().perform()
    # ActionChains(liu).move_to_element_with_offset(img_src,10,int(list_xy[1])).context_click().perform()
    # time.sleep(1)
    # ActionChains(liu).move_by_offset(xoffset=int(list_xy[0])-52,yoffset=1).perform()
    # time.sleep(1)
    # ActionChains(liu).click().perform()
    print(list_xy)
    track = get_track(int(list_xy[0]))
    for i in track:
        time.sleep(0.5)
        action.move_by_offset(xoffset=i - 50, yoffset=0).perform()
        action.reset_actions()
    time.sleep(999)

    pass
